package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author shy
 * @since 2021-05-29
 */
@RestController
@RequestMapping("/eduservice/edu-video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;


    @PostMapping("addVideo")
    @ApiOperation(value = "添加小节")
    public R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }


    @DeleteMapping("deleteVideo/{id}")
    @ApiOperation(value = "删除小节")
    public R deleteVideo(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)){
            // 删除视频
            R result = eduVideoService.removeVideoByCourseId(videoSourceId);
            if (result.getCode() == 20001){
                throw  new GuliException(20001, "删除视频失败，熔断器....");
            }
        }
        // 删除小节
        eduVideoService.removeById(id);
        return R.ok();
    }

    @PostMapping("updateVideo")
    @ApiOperation(value = "修改小节")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }


    @GetMapping("getVideo/{videoId}")
    @ApiOperation(value = "查询小节")
    public R getVideoById(@PathVariable String videoId){
        EduVideo eduVideo = eduVideoService.getById(videoId);
        return R.ok().data("video",eduVideo);
    }


}

